﻿using Collaboration_System.DataBase;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Windows;
using System.Windows.Input;
namespace Collaboration_System
{
    public partial class Autorization : Window
    {
        Collaboration_SystemEntities entity = new Collaboration_SystemEntities();
        Users users = new Users();
        public Autorization()
        {
            InitializeComponent();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {

        }

        private void Border_MouseDown(object sender, MouseButtonEventArgs e)
        {
            if (e.ChangedButton == MouseButton.Left)
            { 
             this.DragMove();
            }
        }

        private void ClouseButton_MouseDown(object sender, MouseButtonEventArgs e)
        {
            Close();
        }

        private void EyeButton_MouseDown(object sender, MouseButtonEventArgs e)
        {

        }

        private void SingIn_Click(object sender, RoutedEventArgs e)
        {
            Registration autorization = new Registration();
            autorization.Show();
            Close();
        }

        private void SingUp_Click(object sender, RoutedEventArgs e)
        {
            string login = Login.Text;
            string password = Password.Password;
            if (entity.Users.Any(u => u.login == login))
            {
                if (entity.Users.Any(u => u.password == password))
                {
                    var userData = entity.Users.FirstOrDefault(u => u.login == login);
                    using (var writer = new StreamWriter(@"config.json"))
                    {
                        writer.WriteLine($"{userData.id}");
                        writer.WriteLine($"{userData.login}");
                        writer.WriteLine($"{userData.Position}");
                        writer.WriteLine($"{userData.email}");
                    }
                    Dashboard autorization = new Dashboard();
                    autorization.Show();
                    Close();
                }
                else
                {
                    MessageBox.Show("Неверный пароль");
                }
            }
            else
            {
              MessageBox.Show("Неверный логин");
            }

        }
    }
}
